<?php
namespace app\wxapp\controller;

use app\model\Record; 
use app\model\Fee; 
use app\model\Comm; 
use app\model\Theme; 
use app\model\Worker; 
use app\model\Fans; 
use app\model\Userdata; 
use app\model\Relation; 

class Getredpackage extends Base
{
    public function Getredpackage()
    {
/**
 * Desc:
 * Time: 2021/7/2 4:04 下午
 */
global $_P, $_G;
$response = modelShow(40000);
$system=$_G['system'];
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);

if($themeid < 0){
    wxapp_result(1, '活动入口不正确');
    exit;
}
$theme = Theme::getThemeById($themeid);
$config =   Theme::getConfig($themeid);
if(empty($theme['id'])){
    wxapp_result(1, '请确认活动地址是否正确');
    exit;
}

if (!$_G['ispost']){
    $response = modelShow(50000);
}else{
    $op = isset($_P['op'])?$_P['op']:'display'; //display默认
    if ($op=='index'){
        $result = [];
        $result['hbopenstyle'] = $config['hbopenstyle'];
        $result['myredbagstyle'] = $config['myredbagstyle'];
        $result['balanceMoney'] = Comm::getCommSuccessOrFail($system,$config,$themeid,$_G['openid'])['total'];

        $company = $theme['jubf'];
        $jump_txt = '';
        $jump_txt_amount = '';
        $red_package = [];
        $middle_data = [];
        $notice1 = '';
        $notice2 = '';
        $into_no_hb = false;
        $endtime =date("Y-m-d H:i:s",$theme['endtime']);
        $commssum_open = Comm::getOpencommssum($config, $themeid);
        if ($commssum_open==0){
            if ($config['hbopenfee2']>0) {
                $red_package = [
                    'company' => $company,
                    'company_icon' => $config['pinpailogo'],
                    'title' => '给您发了一个红包',
                    'content' => '随机拆' . $config['hbopenfee1'] . '-' . $config['hbopenfee2'] . '现金红包'
                ];
                if ($config['hbopenfee1'] == $config['hbopenfee2']) {
                    $red_package['content'] = '随机获得' . $config['hbopenfee1'] . '现金红包';
                }
            }

            $middle_data['first_content'] = '现金红包待领取';
            if ($config['comm12']>$config['hborderfee2'] && $config['comm12']>0){
                $first_amount = $config["comm12"];
                $middle_data['second_title'] = '好友下单';
                $middle_data['second_content'] = '最高可获得现金红包'.$first_amount.'元';
            }elseif ($config['hborderfee2']>0){
                $first_amount = $config["hborderfee2"];
                $middle_data['second_title'] = '现在下单';
                $middle_data['second_content'] = '最高可获得现金红包'.$first_amount.'元';
            }elseif ($config['hbopenfee2']>0){
                $first_amount = $config["hbopenfee2"];
                $middle_data['second_title'] = '商家赠送';
                $middle_data['second_content'] = '商家赠送最高可领'.$first_amount.'元';
            }elseif ($config['comm52']>0){
                $first_amount = max($config['comm52'],$config['comm52_2'],$config['comm52_3']);
                $middle_data['second_title'] = '阅读红包';
                $middle_data['second_content'] = '好友查看阅读最高可领'.$first_amount.'元';
            }elseif ($config['comm42']>0){
                $first_amount = max($config['comm42'],$config['comm42_2'],$config['comm42_3']);
                $middle_data['second_title'] = '转发红包';
                $middle_data['second_content'] = '转发至朋友圈最高可领'.$first_amount.'元';
            }

            $middle_data['first_amount'] = $first_amount.'元';

            $middle_data['right_amount'] = $first_amount;
            $middle_data['right_img'] = 'https://css2020.yixuan.net/2020css/yx2020/images/getredbag.jpg';

        }else{
            $endtime = date('m/d/Y H:i:s',$theme['endtime']);
        }

        if ($config['iscomm']==1  ) {
            if ($config['hbopenfee1']==0 && $config['hbopenfee2']==0){
                $result['hbopenstyle'] = 2;
            }
            if ($config['ismycomm_old']==2 || ($config['hbopenfee1']==0 && $config['hbopenfee2']==0) && ($config['hborderfee1']>0 || $config['hborderfee2']>0)){
                $into_no_hb = true;
            }

            $themyrecord = Record::getLastGroupByUid($_G['member']['uid'],$themeid);
            if ($config['hborderfee2']>0 && empty($themyrecord)){
                $jump_txt = '首单返';
                $jump_txt_amount = $config["hborderfee2"].'元';
            }
            // 一级分销提成
            $comm1_min = Comm::getnumberminormax($config['comm11'],$config['comm11_2'],$config['comm11_3'],"min");
            $comm1_max = Comm::getnumberminormax($config['comm12'],$config['comm12_2'],$config['comm12_3'],"max");
            if ($config['comm4autored']==3 && $config['fenxiao01_hpopen']==1){
                if ($config['comm52']>0){
                    $first_amount = max($config['comm52'],$config['comm52_2'],$config['comm52_3']);
                    $notice1 = [
                        'txt1'=>'每邀请1个新好友，最高获得',
                    ];
                    $notice1['txt2']= $first_amount .'元';
                    $notice1['txt3']= '阅读红包';
                }elseif ($config['comm42']>0){
                    $first_amount = max($config['comm42'],$config['comm42_2'],$config['comm42_3']);
                    $notice1 = [
                        'txt1'=>'将链接转发至朋友圈，最高获得',
                    ];
                    $notice1['txt2']= $first_amount .'元';
                    $notice1['txt3']= '转发红包';
                }

            }else if ($comm1_max>0){
                $notice1 = [
                    'txt1'=>'邀请好友成功下单，每单可获得',
                ];
                if ($comm1_min==$comm1_max){
                    $notice1['txt2']= $comm1_min .'元';
                    $notice1['txt3']= '推荐奖励';
                }else{
                    $notice1['txt2']= $comm1_min . '-' . $comm1_max.'元';
                    $notice1['txt3']= '随机推荐红包';
                }
            }
            $notice2 = [
                'txt1'=>'温馨提示',
                'txt2'=>'账户余额满'.$config["hbtixian"] .'元即可提现，您可以通过',
                'txt3'=>'以下方式提高余额'
            ];
            if ($config['hborderfee2']>0){
                $notice2['img'] =$_G['URL']['YX2020_CSS'].'images/notice2.png';
            }else {
                $notice2['img'] = $_G['URL']['YX2020_CSS'].'images/notice2_noopenhb.png';
            }
        }
        $result['noticeBg'] = ['jump_txt'=>'','jump_txt_amount'=>''];
        $themyrecord = Record::getLastGroupByUid($fans['uid'],$themeid);

        if (!$themyrecord){
            $result['noticeBg'] = ['jump_txt'=>$jump_txt,'jump_txt_amount'=>$jump_txt_amount];
        }else{
            $notice2='';
        }
        //判断当前可提现金额
        if ((float)$result['balanceMoney']>0){
            if ($config['hbtixianmode']){
                //每次提现都限制
                if ((float)$result['balanceMoney']>=(float)$config["hbtixian"]){
                    $notice1 = $notice2 = '';
                }
            }else{
                //只限制第一次
                extract(Comm::getCommSuccessOrFail($system,$config,$themeid,$_G['openid']));
                if ($totalsuccess){
                    //不是第一次
                    $notice1 = $notice2 = '';
                }
            }
        }

        $result['into_no_hb'] = $into_no_hb;
        $result['red_package'] = $red_package;
        $result['middle_data'] = $middle_data;
        $result['notice1'] = $notice1;
        $result['notice2'] = $notice2;
        $result['endtime'] = $endtime;
        return_json(0,'',$result);
    }

    if ($op=='openredbag'){
        if (empty($_G['member']['uid'])){
            return_json(1,'请先前往小程序');
        }
        if(!empty($fans) && !empty($config)){
            $money = Comm::getopenredbag($system,$config);
        }
        if ($money>0){
//            $url = $_G['siteroot'].createMobileUrl('mycomm',array('themeid'=>$themeid));
            $endtime = date('m/d/Y H:i:s',$theme['endtime']);
            $ishbopen = true;
            if ($config['myredbagstyle']==0){

            }
        }else{
            $ishbopen = false;
        }
        $result = ['money'=>sprintf("%.2f",$money),'balanceMoney'=>sprintf("%.2f",$money)];
        if ($ishbopen){
            return_json(0,'',$result);
        }else{
            return_json(1,'拆红包失败，请重试',$result);
        }
    }
    if($op == 'getcarlist'){
        $pageNumber = max(2, intval($_P['pg']));
        $result = Fans::getCarOrderList($themeid,$config,$pageNumber);
        if ($result['total']==0){
            $result['code'] = 0;
        }
        return_json(0,'',$result);
    }

    $p = array(":themeid"=>$themeid);
    $pm = array(":themeid"=>$themeid,":openid"=>$_G['openid']);
    $pu = array(":themeid"=>$themeid,":uid"=>$_G['member']['uid']);
    if ($config['myredbagstyle']==1 && $op == 'display'){
        $op = 'display_myredbag';
    }
//    if ($fans['status']==2 ) {
//        return_json(1,'您还没有可提现的金额！',[]);
//    }

    if ($op == 'getbaseinfo'){
        Userdata::addLog($themeid, $_G['member']['uid'], 13);


        extract(Comm::getCommSuccessOrFail($system,$config,$themeid,$_G['openid']));

        // 一级分销提成
        $comm1_min = Comm::getnumberminormax($config['comm11'],$config['comm11_2'],$config['comm11_3'],"min");
        $comm1_max = Comm::getnumberminormax($config['comm12'],$config['comm12_2'],$config['comm12_3'],"max");

        $tichenglist = str_split($comm1_max, 1);

        if ($comm1_max>0 && $totalsuccess<$config['hbtixian']){
            $neednum = ceil(($config['hbtixian'] - $totalfail)/($comm1_min+$comm1_max)*2);
        }
        $commresult = Comm::getcommslist($system, $config, $themeid);
        $fenxiaotip = $commresult['fenxiaotip'];

        //一级分销区间
        $comm2_min = Comm::getnumberminormax($config['comm21'],$config['comm21_2'],0,"min");
        $comm2_max = Comm::getnumberminormax($config['comm22'],$config['comm22_2'],0,"max");

        $comm3_min = Comm::getnumberminormax($config['comm31'],$config['comm31_2'],0,"min");
        $comm3_max = Comm::getnumberminormax($config['comm32'],$config['comm32_2'],0,"max");

        $comm4_min = Comm::getnumberminormax($config['comm41'],$config['comm41_2'],$config['comm41_3'],"min");
        $comm4_max = Comm::getnumberminormax($config['comm42'],$config['comm42_2'],$config['comm42_3'],"max");

        if ($system['fenxiao07']==1){
            $comm7_min = $comm4_min;
            $comm7_max = $comm4_max;
            $yaoyiyao_max = $comm7_max * $config['comm4sum'];
            $comm4_max = 0;
        }

        $share_max = ($comm4_max * $config['comm4sum']) + $comm1_max;

        if ($system['fenxiao05']==1){
            $comm5_min = Comm::getnumberminormax($config['comm51'],$config['comm51_2'],$config['comm51_3'],"min");
            $comm5_max = Comm::getnumberminormax($config['comm52'],$config['comm52_2'],$config['comm52_3'],"max");
            $yuedu_max = $comm5_max * $config['comm5sum'];
        }
//        $url =  "/". createMobileUrl('index',array('fopenid'=>$_G['openid'],'fromuid'=>$_G['member']['uid'],'workerid'=>$fans['workerid'],'themeid'=>$themeid));

        //获取员工信息
        $canshare = 1;
        if (defined('CANSHARE')){
            if (CANSHARE==0){
                $canshare = 0;
            }
        }
        $themyrecord = Record::getLastGroupByUid($_G['member']['uid'],$themeid);
        if(!empty($themyrecord)){
            $workerid = $themyrecord['workerid'];
            if ($themyrecord['type']==1){
                $recordid = $themyrecord['pid'];
            }else{
                $recordid = $themyrecord['id'];
            }
        }
        if ($workerid==0){
            $relation = Relation::getByThemeId($themeid,$_G['openid']);
            $workerid = $relation['workerid'];
        }
        $theyworker = Worker::getThemeWorkerByUid($workerid,$themeid,1);
        $workername = $theyworker['username'];
        $sharetitle = $theme['title'];
        if (strpos($sharetitle, "WORKER") !== false){
            $sharetitle = str_replace("WORKER",$workername,$sharetitle);
        }elseif (!empty($workername) && $config['isshowkefu']==1){
            $sharetitle = $sharetitle ."【客服:".$workername."】";
        }
        $sharedesc = $theme['desc'];
        if (empty($theme['img'])){
            $shareimgurl = tomedia($config['headlogo']);
        }else{
            $shareimgurl = tomedia($theme['img']);
        }

        $sharedata = array(
            'themeid'=>$themeid,
            'fopenid'=>$_G['openid'],
            'fromuid'=>$_G['member']['uid'],
            'workerid'=>$workerid,
        );
        if (!empty($recordid)){
            $sharedata['recordid']=$recordid;
        }
        if (($fans['status']==2 || ($theme['endtime']+$endday) < TIMESTAMP) && $system['s']['isurl']==1) {
//            $shareurl =   $_G['siteroot'].createMobileUrl('index',$sharedata);
        }else{
            if (empty($myworker)){
//                $shareurl =   $_G['siteroot'].createMobileUrl('index',$sharedata);
            }else{
                $shareurl = str_replace("&recordid=" . $recordid , "",$workerurl);
            }

            if ($system['gongzhong']==1 ){
                $domain3='';
                if ($system['domainpingbi1']==0 && !empty($system['domainhead1'])) {
                    $domain3=$system['domainhead1'];
                }elseif ($system['domainpingbi2']==0 && !empty($system['domainhead2'])) {
                    $domain3=$system['domainhead2'];
                }elseif ($system['domainpingbi3']==0 && !empty($system['domainhead3'])) {
                    $domain3=$system['domainhead3'];
                }
                if (strstr($shareurl,$system['domainhead1'])!==false && $system['domainpingbi1']==1 ){
                    $shareurl = str_replace($system['domainhead1'],$domain3,$shareurl);
                }
                if (!empty($system['domainhead2'])) {
                    if (strstr($shareurl,$system['domainhead2'])!==false && $system['domainpingbi2']==1 ){
                        $shareurl = str_replace($system['domainhead2'],$domain3,$shareurl);
                    }
                }
                if ($system['domainpingbi3']==0 && rand(1, 10) < 6){
                    $shareurl = str_replace($system['domainhead1'],$system['domainhead3'],$shareurl);
                    $shareurl = str_replace($system['domainhead2'],$system['domainhead3'],$shareurl);
                }
            }else{
                if ($system['gongzhong']!=1){
                    if (rand(0, 10)<5){
                        $shareurl = str_replace($system['s']['domain1'],$system['s']['domain3'],$shareurl);
                    }else{
                        $shareurl = str_replace($system['s']['domain1'],$system['s']['domain2'],$shareurl);
                    }
                }
                if (!empty($system['s']['pingbi01'])) {
                    $shareurl = str_replace($system['s']['pingbi01'],$system['s']['domain3'],$shareurl);
                }
                if (!empty($system['s']['pingbi02'])) {
                    $shareurl = str_replace($system['s']['pingbi02'],$system['s']['domain3'],$shareurl);
                }
            }
        }

        $top_info = [];
        $top_info['balanceAmount'] = $comm1_max;
        $top_info['avatar'] = $fans['avatar'];
        if ($totalfail==$total){
            if ($totalfail>=$config['hbtixian']){
                $top_info['can_tixian'] = true;
            }else{
                $top_info['can_tixian'] = false;
            }
        }else{
            if ($config['hbtixianmode']==1){
                if ($totalfail>=$config['hbtixian']){
                    $top_info['can_tixian'] = true;
                }else{
                    $top_info['can_tixian'] = false;
                }
            }else{
                $top_info['can_tixian'] = true;
            }
        }
        if ($comm1_max>0){
            if (($config['hbopenfee1']>0 && $system['autored']==0 ) || $system['autored']==2){
                $top_info['left_title'] = '未提现';
                $top_info['left_amount'] = $totalfail;
            }
            $top_info['right_title'] = '累计获得';
            $top_info['right_amount'] = $total;
            $top_info['amount_imgs'] = [];
            foreach ($tichenglist as $index => $item){
                if ($item =='.'){
                    $temp_img = $_G['URL']['YX2020_CSS'].'images/cir.png';
                }else{
                    $temp_img = $_G['URL']['YX2020_CSS']. 'images/'.$item.'.png';
                }
                array_push($top_info['amount_imgs'],$temp_img);
            }
            $top_info['middle_content'] = [];
            if ($neednum>0 && (($config['hbopenfee1']>0 && $system['autored']==0 )|| $system['autored']==2)) {
                $top_info['middle_content'] = [
                    'txt1'=>'再邀请',
                    'txt2'=>$neednum.'位好友下单',
                    'txt3'=>'就可以提现了'
                ];
            }
        }else{
            if (($config['hbopenfee1']>0 && $system['autored']==0 )|| $system['autored']==2) {
                $top_info['left_title'] = '未提现';
                $top_info['left_amount'] = $totalfail;
                $top_info['right_title'] = '累计获得';
                $top_info['right_amount'] = $total;
            }else{
                $top_info['right_title'] = '累计获得';
                $top_info['right_amount'] = $total;
            }
        }

        //赚红包玩法
        $method = [];
        $method['title'] = '赚红包玩法';
        $method['list'] = [];
        if ($config['hborderfee2']>0){
            $temp_list_item = [
                'img'=> $_G['URL']['YX2020_CSS']. 'images/g7.png',
                'title'=>'购买商品',
                'amount'=>'+'.$config['hborderfee2'].'元'
            ];
            if ($config['hborderfee1']!= $config['hborderfee2']){
                $temp_list_item['content']='首次购买商品，可返'.$config['hborderfee1'].'-'.$config['hborderfee2'].'元随机红包';
            }else{
                $temp_list_item['content']='首次购买商品，可返'.$config['hborderfee2'].'元红包';
            }
            if (empty($recordid)){
                $temp_list_item['btn']=['type'=>'jump','method'=>'index','text'=>'去购买'];
            }else{
                $temp_list_item['btn']=['type'=>'end','method'=>'','text'=>'已完成'];
            }
            array_push($method['list'],$temp_list_item);
        }
        if ($config['comm12']>0){
            $temp_list_item = [
                'img'=> $_G['URL']['YX2020_CSS']. 'images/g8.png',
                'title'=>'好友下单',
                'amount'=>'+'.$comm1_max.'元'
            ];
            if ($comm1_min != $comm1_max){
                $temp_list_item['content']='每成功邀请1位好友下单，单笔可得'.$comm1_min.'-'.$comm1_max.'元随机红包';
            }else{
                $temp_list_item['content']='每成功邀请1位好友下单，单笔可得'.$comm1_max.'元';
            }
            $temp_list_item['btn']=['type'=>'share','method'=>'fenxiao','text'=>'去邀请'];
            array_push($method['list'],$temp_list_item);
        }
        if ($config['comm22']>0){
            $temp_list_item = [
                'img'=> $_G['URL']['YX2020_CSS']. 'images/g8.png',
                'title'=>'二级分销',
                'amount'=>'+'.$comm2_max.'元'
            ];
            if ($comm2_min != $comm2_max){
                $temp_list_item['content']='你推荐A，A推荐B，好友B下单，您单笔可得'.$comm2_min.'-'.$comm2_max.'元随机红包';
            }else{
                $temp_list_item['content']='你推荐A，A推荐B，好友B下单，您单笔可得'.$comm2_max.'元';
            }
            $temp_list_item['btn']=['type'=>'share','method'=>'fenxiao','text'=>'去邀请'];
            array_push($method['list'],$temp_list_item);
        }
        if ($config['comm32']>0){
            $temp_list_item = [
                'img'=> $_G['URL']['YX2020_CSS']. 'images/g8.png',
                'title'=>'三级分销',
                'amount'=>'+'.$comm3_max.'元'
            ];
            if ($comm3_min != $comm3_max){
                $temp_list_item['content']='你推荐A，A推荐B，B推荐C，好友C下单，您单笔可得'.$comm3_min.'-'.$comm3_max.'元随机红包';
            }else{
                $temp_list_item['content']='你推荐A，A推荐B，B推荐C，好友C下单，您单笔可得'.$comm3_max.'元';
            }
            $temp_list_item['btn']=['type'=>'share','method'=>'fenxiao','text'=>'去邀请'];
            array_push($method['list'],$temp_list_item);
        }
        if ($share_max>0){
            $temp_list_item = [
                'img'=> $_G['URL']['YX2020_CSS']. 'images/g9.png',
                'title'=>'分享朋友圈',
                'amount'=>'+'.$share_max.'元'
            ];
            if ($comm4_max>0 && $comm1_max>0){
                if ($comm4_min != $comm4_max){
                    $temp_list_item['content']='将链接分享到朋友圈，即可得'.$comm4_min.'-'.$comm4_max.'元的分享红包，';
                }else{
                    $temp_list_item['content']='将链接分享到朋友圈，即可得'.$comm4_max.'元的分享红包，';
                }
                if ($config['comm4sum']>1){
                    if ($config['comm4daynum']!=$config['comm4sum']){
                        $temp_list_item['content'].='每天可获得'.$config['comm4daynum'].'个,';
                    }else{
                        $temp_list_item['content'].='累计可获得'.$config['comm4sum'].'个,且';
                    }
                }
                if ($comm1_min != $comm1_max){
                    $temp_list_item['content'].='好友通过您的链接下单，您每笔还可得'.$comm1_min.'-'.$comm1_max.'元的拥金红包';
                }else{
                    $temp_list_item['content'].='好友通过您的链接下单，您每笔还可得'.$comm1_max.'元的拥金红包';
                }
            }elseif ($comm4_max>0){
                if ($comm4_min != $comm4_max){
                    $temp_list_item['content']='将链接分享到朋友圈，即可得'.$comm4_min.'-'.$comm4_max.'元';
                }else{
                    $temp_list_item['content']='将链接分享到朋友圈，即可得'.$comm4_max.'元';
                }
                if ($config['comm4sum']>1){
                    if ($config['comm4daynum']!=$config['comm4sum']){
                        $temp_list_item['content'].='，每天可获得'.$config['comm4daynum'].'个';
                    }
                    $temp_list_item['content'].='，累计可获得'.$config['comm4sum'].'个';
                }
            }elseif ($comm1_max>0){
                if ($comm1_min != $comm1_max){
                    $temp_list_item['content']='将链接分享到朋友圈，好友通过您的链接下单，每笔可得'.$comm1_min.'-'.$comm1_max.'元';
                }else{
                    $temp_list_item['content']='将链接分享到朋友圈，好友通过您的链接下单，每笔可得'.$comm1_max.'元';
                }
            }
            if ($comm4_max>0){
                $temp_list_item['btn']=['type'=>'moments','method'=>'zhuanfa','text'=>'去分享'];
            }elseif ($comm1_max>0){
                $temp_list_item['btn']=['type'=>'share','method'=>'fenxiao','text'=>'去分享'];
            }

            array_push($method['list'],$temp_list_item);
        }
        if ($yuedu_max>0){
            $temp_list_item = [
                'img'=> $_G['URL']['YX2020_CSS']. 'images/g9.png',
                'title'=>'好友查阅',
                'amount'=>'+'.$yuedu_max.'元'
            ];
            if ($comm7_min != $comm7_max){
                $temp_list_item['content']='将活动链接分享给好友或朋友圈，新好友查阅，可得'.$comm5_min.'-'.$comm5_max.'元随机红包';
            }else{
                $temp_list_item['content']='将活动链接分享给好友或朋友圈，新好友查阅，可得'.$comm5_max.'元';
            }
            if ($config['comm5sum']>1){
                if ($config['comm5daynum']!=$config['comm5sum']){
                    $temp_list_item['content'].='每天可获得'.$config['comm5daynum'].'个';
                }
                $temp_list_item['content'].='，累计可获得'.$config['comm5sum'].'个';
            }
            $temp_list_item['btn']=['type'=>'share','method'=>'yuedu','text'=>'去邀请'];
            array_push($method['list'],$temp_list_item);
        }
        if ($comm7_max>0){
            $temp_list_item = [
                'img'=> $_G['URL']['YX2020_CSS']. 'images/g9.png',
                'title'=>'摇一摇红包',
                'amount'=>'+'.$yaoyiyao_max.'元'
            ];
            if ($comm7_min != $comm7_max){
                $temp_list_item['content']='进入活动链接，手机摇一摇，可得'.$comm7_min.'-'.$comm7_max.'元，将链接分享到朋友圈即可到账';
            }else{
                $temp_list_item['content']='进入活动链接，手机摇一摇，可得'.$comm7_max.'元，将链接分享到朋友圈即可到账';
            }
            if ($config['comm4sum']>1){
                if ($config['comm4daynum']!=$config['comm4sum']){
                    $temp_list_item['content'].='，每天可获得'.$config['comm4daynum'].'个';
                }
                $temp_list_item['content'].='，累计可获得'.$config['comm4sum'].'个';
            }
            $temp_list_item['btn']=['type'=>'jump','method'=>'index','text'=>'摇一摇'];
            array_push($method['list'],$temp_list_item);
        }
        $height = (186*count($method['list']))+40;
        $method['height'] = $height;
//赚红包玩法

        $desc = [];
        $activity = [];
        //        活动主办方
        $activity = [
            'title'=>'活动主办方',
            'content1'=>$theme['jubf'],
            'content2'=>'地址：'.$config['province'].$config['city'].$config['district'].$config['addresstxt'],
            'content3'=>'电话：'.$theme['mobile']
        ];
        if ($system['autored']!=3){
            $desc['title'] = '提现说明';
            $desc['list'] = [];
            if (($config['hbopenfee1']>0 && $system['autored']==0 )|| $system['autored']==2){
                $desc['list'][] = '红包余额满'.$config['hbtixian'].'元,即可提现';
                $desc['list'][] = '每日可提现5次，单笔最高可提现500.00元';
            }
            if ($system['autored']==0){
                $desc['list'][] = '红包需经核实无误后，到店领取或线上发放，请耐心等待通知';
            }else if ($system['autored']==2){
                $desc['list'][] = '红包需您手动提现，提现后5分钟内到帐，如延迟请耐心等待';
                $desc['list'][] = '红包将发送至您的微信零钱，请到微信钱包中进行查看';
            }else{
                $desc['list'][] = '红包将在5分钟内到账，如遇高峰期，可能延迟到账，请耐心等待';
                $desc['list'][] = '红包将发送至您的微信零钱，请到微信钱包中进行查看';
            }
            $desc['btn_txt'] = '请实名认证您的微信账号，否则红包无法到账';
        }else{
            //        说明
            $desc['title'] = '红包说明';
            $desc['list'] = [];
            array_push($desc['list'],['content'=>'红包请按活动规定时间及规则，到店内兑换；']);
            $desc['btn_txt'] = '';

        }
//        温馨提示
        $notice['title'] = '温馨提示';
        if ($config['hborderfee1']>0 &&  empty($recordid)){
            $notice['content1'] = '余额满';
            $notice['content2'] = $config['hbtixian'].'元';
            $notice['content3'] = '才可提现哦，自己下单和邀请好友下单均有机会获得下单奖励！';
            $notice['left_btn'] = '下单';
            $notice['right_btn'] = '分享';
        }else{
            $notice['content1'] = '余额满';
            $notice['content2'] = $config['hbtixian'].'元';
            $notice['content3'] = '才可提现哦，邀请好友下单最多可获得'.$comm1_max.'元奖励！';
            $notice['left_btn'] = '';
            $notice['right_btn'] = '去邀请';
        }
        if ($config['iscommshow']>0){
            $comminfo = Comm::getcomminfo($system, $config, $themeid,20);
            $comminfo['iscommshow']=$config['iscommshow'];
            $comminfo['iscommshow24']=0;
            $comminfo['show_more']=$config['iscommshow_more']==0?2:4;
            $comminfo['myrank']= Comm::getcommrank($config,$themeid);

            if($config['iscomm']==1 || $config['shopsort'] > 0) {
                if (!empty($config['commlistimg'])) {
                    $comminfo['commlistimg'] = $config['commlistimg'];
                } else {
                    $comminfo['commlistimg'] = $_G['URL']['FCPDD_CSS'] . "images/phb.png";
                }
                if (!empty($config['commlistimgbg'])) {
                    $comminfo['commlistimgbg'] = $config['commlistimgbg'];
                } else {
                    $comminfo['commlistimgbg'] = $_G['URL']['FCPDD_CSS'] . "images/dbg.jpg";
                }

                $comminfo['commlistimg'] = str_replace("http://", "https://", $comminfo['commlistimg']);
                $comminfo['commlistimgbg'] = str_replace("http://", "https://", $comminfo['commlistimgbg']);
                $comminfo['isrankrenshu'] = $config['isrankrenshu'];
                $comminfo['isrankqianshu'] = $config['isrankqianshu'];

                $comminfo['xuanzename']='选择';
                $comminfo['liulanname']='人气值';
            }
        }
        $result = [
            'top_info'=>$top_info,
            'method'=>$method,
            'desc'=>$desc,
            'activity'=>$activity,
            'notice'=>$notice,
            'comminfo'=>$comminfo,
        ];
        return_json(0,'',$result);
    }

    elseif ($op =='tixian'){
        $code=0;
        if ($system['autored']==2) {
            $errormsg = '';
            $begintime1 = $theme['begintime'];
            $endime1 = $theme['endtime'];
            if($begintime1 > TIMESTAMP){
                $errormsg =  '活动尚未开始，敬请期待~';
                return_json(0,'',array('code'=>0,'errormsg'=>$errormsg));
            }
            if($endime1 < TIMESTAMP ||  $theme['status'] != 1){
                $errormsg =  '活动已经结束啦，感谢您的支持';
                return_json(0,'',array('code'=>0,'errormsg'=>$errormsg));
            }

            //            0开团红包 1参团红包 2二级红包 3三级红包 4 转发红包 5阅读红包 6员工提成 7摇一摇红包 8补余款 9单买 10进入链接获得 11自己报名获得 12代理提成 13抽奖红包提现 98待提现 99手动提现
            $hongbaocishu = (int)db_fetchcolumn ( 'SELECT count(id) FROM ' . tablename (t_comm) . ' as m 
                    WHERE m.themeid = :themeid and type=99 and addtime>=' . strtotime(date("Y-m-d")) . ' and m.openid = :openid',$pm);

            if ($config['comm4autored']==3){
                $totalsuccess = (float)db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
                    WHERE m.themeid = :themeid and (status=1 or status=98 and type=99) and type in (0,1,2,3,4,5,7,8,9,10,11,15) and m.openid = :openid',$pm);

                $totalfail = (float)db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
                        WHERE m.themeid = :themeid and status=0 and type<>99 and m.openid = :openid',$pm);
            }else{
                $totalsuccess = (float)db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
                    WHERE m.themeid = :themeid and (status=1 or status=98 and type=99) and type in (0,1,2,3,8,9,10,11,15) and m.openid = :openid',$pm);

                $totalfail = (float)db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
                        WHERE m.themeid = :themeid and status=0 and type in (0,1,2,3,8,9,10,11,15) and m.openid = :openid',$pm);
            }

            $totalopenhb = (float)db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
                        WHERE m.themeid = :themeid and status=0 and type=10 and m.openid = :openid',$pm);

            if ($totalopenhb>=$config['hbtixian']){
                $totalfail = $totalfail-$totalopenhb;
            }
            if ($totalfail<0.3 && $system['send_mode']!=1) {
                $errormsg = '未达到提现金额';
                return_json(0,'',array('code'=>0,'errormsg'=>$errormsg));
            }
            if ($totalfail>500) {
                $errormsg = '金额大于500元需联系工作人员提现';
                return_json(0,'',array('code'=>0,'errormsg'=>$errormsg));
            }
            if ($hongbaocishu>5){
                $errormsg = '今日已提现'. $hongbaocishu .'次已达上限，明日再来提现' ;
                return_json(0,'',array('code'=>0,'errormsg'=>$errormsg));
            }

            $totalsuccess = sprintf("%.2f", $totalsuccess);
            $totalfail = sprintf("%.2f", $totalfail);
            if ($totalfail<$config['hbtixian']) {
                if ($totalsuccess<$config['hbtixian']) {
                    $errormsg = '未达到提现金额';
                    return_json(0,'',$result);
                }elseif($config['hbtixianmode'] == 1){
                    $errormsg = '未达到提现金额';
                    return_json(0,'',array('code'=>0,'errormsg'=>$errormsg));
                }
            }
            if ($errormsg=="") {
                $result = Comm::addhbtixian($system,$config,$totalfail);
                if ($result!=false && intval($result)>0) {
//                $row = Comm::getCommtixian($result);
//                $r = array();
//                if(!empty($row)){
//                    $settings = "commid:".$row['id'];
//                    $r = Fee::commRecord($row,$system);
//                    if ($r[0]==0){
                    $errormsg="提现成功，1分钟左右到账;可在微信零钱中查看到账信息";
                    return_json(0,'',array('code'=>1,'errormsg'=>$errormsg));
//                    }
//                }
                }
            }
        }

        elseif ($system['autored']==0) {
            $errormsg = '需要等待管理员审核后发放';
        }else{
            $errormsg = '无权操作';
        }
        $result = array();
        $result['errormsg']=$errormsg;
        $result['code']=$code;
        return_json(0,'',$result);
    }
//     获取红包记录
    elseif ($op=='get_myredbag'){
        //        红包记录
        $flag = [
            'title'=>'红包记录'
        ];
        $pageNumber = max(1, intval($_P['pg']));
        $pageSize = 20;
        if (!($config['iscommshowfail'] == 1  || ($config['hbopenfee1']>0 && $system['autored']==0 )|| $system['autored']==2)){
            $comm1txt=" and status=1 ";
        }
        if ($config['iscommshowfail'] == 0){
            $comm2txt=" and status=1 ";
        }else{
            $comm2txt=" and status<2 ";
        }

        $sql="SELECT * FROM (
                select type,fee,fromuid,uid,addtime,msg,status,1 as comm
                from ".tablename(t_comm)."
                WHERE  themeid = :themeid and uid = :uid ".$comm1txt."
                union all
                select type,fee,fromuid,uid,addtime,msg,status,2 as comm 
                from ".tablename(t_commshare)."
                WHERE  themeid = :themeid and  uid = :uid and type<>4 and type<>5 and type<>7 ".$comm2txt.") as t
                ORDER BY addtime desc,fee desc,comm
                LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;
        $list = db_select($sql,$pu);



        foreach ($list as $index => $row){
            if ($row['type']==4 || $row['type'] ==  7){
                $commfanslist = Fans::getById($row['uid'],$themeid);
            }else{
                $commfanslist = Fans::getById($row['fromuid'],$themeid);
            }
            $list[$index]['time'] = date("m-d H:i:s",$row['addtime']);
            $list[$index]['avatar'] = $commfanslist['avatar'];
            $list[$index]['nickname'] = mb_substr($commfanslist['nickname'],0,9,'utf-8');
            if ($row['type'] ==  0) {
                $list[$index]['leixing'] = '好友下单';
            }else if ($row['type'] ==  1) {
                $list[$index]['leixing'] = '好友下单';
            }else if ($row['type'] ==  2) {
                $list[$index]['leixing'] = '二级分销';
            }else if ($row['type'] ==  3) {
                $list[$index]['leixing'] = '三级分销';
            }else if ($row['type'] ==  4 && $row['comm'] ==  1) {
                $list[$index]['leixing'] = '转发红包';
            }else if ($row['type'] ==  4 && $row['comm'] ==  2) {
                $list[$index]['leixing'] = '转发记录';
            }else if ($row['type'] ==  5) {
                $list[$index]['leixing'] = '阅读红包';
            }else if ($row['type'] ==  6) {
                $list[$index]['leixing'] = '员工提成';
            }else if ($row['type'] ==  7 && $row['comm'] ==  1) {
                $list[$index]['leixing'] = '摇一摇红包';
            }else if ($row['type'] ==  7 && $row['comm'] ==  2) {
                $list[$index]['leixing'] = '摇一摇记录';
            }else if ($row['type'] ==  10) {
                $list[$index]['leixing'] = '进入链接';
            }else if ($row['type'] ==  11) {
                $list[$index]['leixing'] = '自己报名';
            }else if ($row['type'] ==  12) {
                $list[$index]['leixing'] = '代理提成';
            }else if ($row['type'] ==  99 && $row['status']==98) {
                $list[$index]['leixing'] = '等待到账';
            }else if ($row['type'] ==  99 && $row['status']==99) {
                $list[$index]['leixing'] = '提现到账';
            }else if ($row['type'] ==  9) {
                $list[$index]['leixing'] = '好友下单';
            }else{
                $list[$index]['leixing'] = '其它';
            }
            $sort = ($pageNumber - 1) * $pageSize + $index+1;
            $list[$index]['sort'] = $sort;
            $list[$index]['fee'] = $row['fee'].'元';

            if ($row['comm']==1 && $row['status']==0 && $row['msg']!=null && $row['msg']!='待客户提现') {
                $list[$index]['status'] = 0;
                $list[$index]['status_txt'] = '提现失败';
            }else if ($row['comm']==1 && $row['status']==0) {
                $list[$index]['status'] = 1;
                $list[$index]['status_txt'] = '未提现';
            }else if ($row['comm']==1 && $row['status']==1) {
                $list[$index]['status'] = 2;
                $list[$index]['status_txt'] = '已提现';
            }
        }
        $sql="SELECT COUNT(*) FROM (
                select type,fee,fromuid,uid,addtime,msg,status 
                from ".tablename(t_comm)."
                WHERE  themeid = :themeid and  uid = :uid
        
                union all
                select type,fee,uid,uid,addtime,msg,status 
                from ".tablename(t_commshare)."
                WHERE  themeid = :themeid and  uid = :uid and status<2) as t
        
                ORDER BY addtime desc,fee desc";

        $total = (int)db_fetchcolumn ($sql ,$pu);

        if ($total==0){
            $result['code'] = 0;
        }elseif ($total<=$pageNumber*$pageSize){
            $result['isend'] = 1;
        }
        $result['list'] = $list;

        return_json(0,'',$result);
    }
    elseif ($op=='display'){
        $totalsuccess = (float)db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
	            WHERE m.themeid = :themeid and (status=1 or status=98 and type=99) and m.openid = :openid',$pm);
        if ($config['iscommshowfail'] == 1 || ($config['hbopenfee1']>0 && $system['autored']==0 ) || $system['autored']==2){
            if ($config['comm4autored']==3){
                $totalfail = (float)db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
			            WHERE m.themeid = :themeid and status=0 and type<>99 and m.openid = :openid',$pm);
            }else{
                $totalfail = (float)db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
			            WHERE m.themeid = :themeid and status=0 and type<>4 and type<>7 and type<>99 and m.openid = :openid',$pm);
            }
        }else{
            $totalfail=0;
        }

        $totalsuccess = $totalsuccess;
        $totalfail = $totalfail;
        $total = $totalsuccess+$totalfail;
        $result = [
            'totalsuccess'=>$totalsuccess,
            'totalfail'=>$totalfail,
            'total'=>$total
        ];
        wxapp_result(0, '',$result);
    }
    elseif ($op == 'getcomm'){
        $pageNumber = max(1, intval($_P['pg']));
        $pageSize = 20;

        if (!($config['iscommshowfail'] == 1  || ($config['hbopenfee1']>0 && $system['autored']==0 ) || $system['autored']==2)){
            $comm1txt=" and status=1 ";
        }
        if ($config['iscommshowfail'] == 0){
            $comm2txt=" and status=1 ";
        }else{
            $comm2txt=" and status<2 ";
        }
        $sql="SELECT * FROM (
                select type,fee,fromuid,uid,addtime,msg,status,1 as comm
                from ".tablename(t_comm)."
                WHERE  themeid = :themeid and uid = :uid ".$comm1txt."
        
                union all
                select type,fee,fromuid,uid,addtime,msg,status,2 as comm 
                from ".tablename(t_commshare)."
                WHERE  themeid = :themeid and  uid = :uid and type<>4 and type<>7 ".$comm2txt.") as t
        
                ORDER BY addtime desc,fee desc,comm
                LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;
        $list = db_select($sql,$pu);

        foreach ($list as $index => $row){
            if ($row['type']==4 || $row['type'] ==  7){
                $commfanslist = Fans::getById($row['uid'],$themeid);
            }else{
                $commfanslist = Fans::getById($row['fromuid'],$themeid);
            }
            $list[$index]['time'] = date("Y-m-d H:i:s",$row['addtime']);
            $list[$index]['avatar'] = $commfanslist['avatar'];
            $list[$index]['nickname'] = mb_substr($commfanslist['nickname'],0,9,'utf-8');
            if ($row['type'] ==  0) {
                $list[$index]['leixing'] = '好友<br>下单';
            }else if ($row['type'] ==  1) {
                $list[$index]['leixing'] = '好友<br>下单';
            }else if ($row['type'] ==  2) {
                $list[$index]['leixing'] = '二级<br>分销';
            }else if ($row['type'] ==  3) {
                $list[$index]['leixing'] = '三级<br>分销';
            }else if ($row['type'] ==  4 && $row['comm'] ==  1) {
                $list[$index]['leixing'] = '转发<br>红包';
            }else if ($row['type'] ==  4 && $row['comm'] ==  2) {
                $list[$index]['leixing'] = '转发<br>记录';
            }else if ($row['type'] ==  5) {
                $list[$index]['leixing'] = '阅读<br>红包';
            }else if ($row['type'] ==  6) {
                $list[$index]['leixing'] = '员工<br>提成';
            }else if ($row['type'] ==  7 && $row['comm'] ==  1) {
                $list[$index]['leixing'] = '摇一摇<br>红包';
            }else if ($row['type'] ==  7 && $row['comm'] ==  2) {
                $list[$index]['leixing'] = '摇一摇<br>记录';
            }else if ($row['type'] ==  10) {
                $list[$index]['leixing'] = '进入<br>链接';
            }else if ($row['type'] ==  11) {
                $list[$index]['leixing'] = '自己<br>报名';
            }else if ($row['type'] ==  99 && $row['status']==98) {
                $list[$index]['leixing'] = '等待<br>到账';
            }else if ($row['type'] ==  99 && $row['status']==99) {
                $list[$index]['leixing'] = '提现<br>到账';
            }else if ($row['type'] ==  9) {
                $list[$index]['leixing'] = '好友<br>下单';
            }else{
                $list[$index]['leixing'] = '其它';
            }
        }
        $sql="SELECT COUNT(*) FROM (
                select type,fee,fromuid,uid,addtime,msg,status 
                from ".tablename(t_comm)."
                WHERE  themeid = :themeid and  uid = :uid
        
                union all
                select type,fee,uid,uid,addtime,msg,status 
                from ".tablename(t_commshare)."
                WHERE  themeid = :themeid and  uid = :uid and status<2) as t
        
                ORDER BY addtime desc,fee desc";

        $total = (int)db_fetchcolumn ($sql ,$pu);

        if ($total==0){
            $result['code'] = 0;
        }elseif ($total<=$pageNumber*$pageSize){
            $result['isend'] = 1;
        }
        $result['data'] = $list;
        return_json(0,'',$result);
    }
    elseif ($op == 'get_rank'){
        $pageNumber = intval($_P['page']);
        $pageSize = 20 ;
        $result = Comm::getcommslist($system, $config, $themeid,($pageNumber - 1) * $pageSize . ',' . $pageSize);

        if ($config['iscommshowworker'] == 1) {
            $workerkey = Worker::getWorkerListSql(' and uid not');
        } elseif ($config['iscommshowworker'] == 2) {
            $workerkey = Worker::getWorkerListSql(' and uid not',' where level<=3 ');
        } else {
            $workerkey = "";
        }

        if ($config['iscommshowfail'] == 0) {
            $total = (int)db_fetchcolumn("SELECT COUNT(*) FROM " . tablename(t_fans) . "
                    WHERE themeid =" . $themeid . " and feesum >0 " . $workerkey . " ");
        }else{
            $total = (int)db_fetchcolumn("SELECT COUNT(*) FROM " . tablename(t_comm) . "
                        WHERE themeid = " . $themeid . " and type <> 99  " . $workerkey . "
                        group by uid");
        }
        if ($pageSize*$pageNumber>=$total || $pageNumber>5){
            $show_more = 3;
        }

        $result = [
            'list'=>$result['commslist'],
            'show_more'=>$show_more,
        ];
        return_json(0,'',$result);
    }
}
wxapp_result(0, '',$result);

}
}